package com.srit.map.web.action;


import atg.taglib.json.util.JSONException;


public class PositionGet {

	public PositionGet(){
		
	}
	public static String getX(String key) throws JSONException{
		HttpClient client = new HttpClient();
		String json = client.notify("http://api.map.baidu.com/geocoder?address="+key+"&output=json&key=37492c0ee6f924cb5e934fa08c6b1676");
		String x = "";
		if(json.indexOf("location")!=-1){
		json=json.substring(json.indexOf("location"));
		x=json.substring(json.indexOf("lng")+5,json.indexOf(","));
		String y=json.substring(json.indexOf("lat")+5,json.indexOf("}"));

		System.out.print(x+",");
		System.out.print(y);
		json=key+":"+x+","+y;
		//System.out.print("/n");
		//json.split(regex)
		//System.out.print(json);
		//json = json.substring(json.indexOf("(")).replace("(", "").replace(")", "");
//		//System.out.println(json);
//		JSONArray _object; 
//		String pos = "";
//		for(int i = 0;i<_object.length();i++){
//			if(_object.getJSONObject(i).toString().contains("result")){
//				System.out.println(_object.getJSONObject(i).getString("name") + 
//						_object.getJSONObject(i).getJSONObject("ext").getJSONObject("detail_info").getJSONObject("point")
//						+_object.getJSONObject(i).getJSONObject("ext").getJSONObject("detail_info").getString("poi_address")
//						);
//				pos = _object.getJSONObject(i).getJSONObject("result").getJSONObject("point").toString();
//				;
//				break;
//			}
//		}
		}
		return x;
	}
	
	public static String getY(String key) throws JSONException{
		HttpClient client = new HttpClient();
		String json = client.notify("http://api.map.baidu.com/geocoder?address="+key+"&output=json&key=37492c0ee6f924cb5e934fa08c6b1676");
		String y = "";
		if(json.indexOf("location")!=-1){
		json=json.substring(json.indexOf("location"));
		String x=json.substring(json.indexOf("lng")+5,json.indexOf(","));
		y=json.substring(json.indexOf("lat")+5,json.indexOf("}"));

		System.out.print(x+",");
		System.out.print(y);
		json=key+":"+x+","+y;
		//System.out.print("/n");
		//json.split(regex)
		//System.out.print(json);
		//json = json.substring(json.indexOf("(")).replace("(", "").replace(")", "");
//		//System.out.println(json);
//		JSONArray _object; 
//		String pos = "";
//		for(int i = 0;i<_object.length();i++){
//			if(_object.getJSONObject(i).toString().contains("result")){
//				System.out.println(_object.getJSONObject(i).getString("name") + 
//						_object.getJSONObject(i).getJSONObject("ext").getJSONObject("detail_info").getJSONObject("point")
//						+_object.getJSONObject(i).getJSONObject("ext").getJSONObject("detail_info").getString("poi_address")
//						);
//				pos = _object.getJSONObject(i).getJSONObject("result").getJSONObject("point").toString();
//				;
//				break;
//			}
//		}
		}
		return y;
	}
}
